package main

import (
	"fmt"
	"fronter/config"
	"fronter/etcdgw"
	"fronter/msgproto"
	"strconv"
	"time"

	"github.com/coreos/etcd/clientv3"
	"github.com/sirupsen/logrus"
	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/reflect/protoreflect"
)

func DoPack(pack *msgproto.TransPack, m protoreflect.ProtoMessage) {
	buffer, err := proto.Marshal(m)
	if err != nil {
		fmt.Println("err! ", err)
		return
	}
	pack.Package = buffer
	fmt.Println("pack succ, size:", len(pack.Package))
}

func look(ev *clientv3.Event) {
	logrus.Infoln(ev.Type, ", k:", string(ev.Kv.Key), " , val:", string(ev.Kv.Value))
}

func main() {
	logrus.Infoln(config.Get().Etcd)
	etcdgw.Watch("aaa", look)
	for i := 0; i < 100; i++ {
		t1 := time.Now() // get current time
		etcdgw.SetKV("aaa", strconv.Itoa(i))
		set_elapsed := time.Since(t1)
		t2 := time.Now()
		val := *etcdgw.GetKey("aaa")
		get_elapsed := time.Since(t2)
		logrus.Infoln(val, " get cost:", get_elapsed, " set cost:", set_elapsed)
	}

	/*var req msgproto.UserLoginReq
	req.Userid = 123

	var pack msgproto.TransPack
	pack.Seqid = uint64(time.Now().UnixNano())
	pack.ProtoName = "aaa"

	DoPack(&pack, &req)

	fmt.Println(string(pack.ProtoReflect().Descriptor().Name()))*/

}
